#! C:\Perl\bin\perl.exe

# Copyright (c) 2010, College Center for Library Automation

# See the included Copyright.txt file for full license

#
#
#

use strict;
use warnings;

use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;
use Date::Calc qw(Decode_Month Add_Delta_YM Add_Delta_Days Today);
use Text::CSV;

use avsCommon;

dbsetup();

my ($content, $response, $request, $url);
my ($period, $custid);
my ($runyear, $runmonth, $runday);

# create user agent, set cookies
my $ua = new LWP::UserAgent(keep_alive=>1);
$ua->timeout(300);
$ua->cookie_jar( {} );

# loop through all the colleges
do {
	getLogin ();
	getStats ();
	logout ();
	
} while $sthlogin->fetch;

dbshutdown();

exit();

sub getLogin {
	# login to get cookies setup cookies
	$url = 'https://secure.safaribooksonline.com/bo3/BO.asp';
	$request = HTTP::Request->new(POST => $url);
	$request->content_type('application/x-www-form-urlencoded');
	$request->content("usr=$username&pwd=$password");

	$response = $ua->request($request);
	if (!$response->is_success && substr($response->status_line,0,3) ne '302') {
		die "#error:$0: POST report failed $url : " . $response->status_line;
	}

}
sub getStats {

	#ask for the 'id' that is used later
	$url = 'https://secure.safaribooksonline.com/bo3/BO.asp?select=Select&Template=_UserReports&id=2076075';
	$response = $ua->request(GET $url);
	if (!$response->is_success) {
		die "#error:$0: GET MENU $url : " . $response->status_line;
	};

	$response->decoded_content =~ /name="id" value="(?<id>.*?)" /s;
	my $id = $+{id};
	
	# ask for the report
	$url = 'https://secure.safaribooksonline.com/bo3/BO.asp';
	$request = HTTP::Request->new(POST => $url);
	$request->content_type('application/x-www-form-urlencoded');
	$request->content("select=Select&Template=_UserReports&id=$id&outputtype=4&Step1_AccountId=0&Step3_UserAccountId=0&Step4_in_counterreport6_startmonth=$begyear%2F$begmonth%2F$begday&Step4_in_counterreport6_tomonth=$endyear%2F$endmonth%2F$endday&run=Run+Now");

	$response = $ua->request($request);
	if (!$response->is_success && substr($response->status_line,0,3) ne '302') {
		die "#error:$0: POST report failed $url : " . $response->status_line;
	}
	
	$response->decoded_content =~ /CreatorId=(?<id>.*?)"/s;	#"
	$url='https://secure.safaribooksonline.com/bo3/bo.asp?search=Search&Template=_UserReports&filter=1&CreatorId=' . $+{id};
	
	print "$url\n";
	print $response->decoded_content;
	
	# after submitting a report, need to wait until it is done. this loop sleeps for 2,4,8,16,32 seconds and will
	# try five times for finished report. once it finds 'done' it grabs the data
	my $sleeptime = 2;
	my $data = '';

	do {

		sleep $sleeptime;
		
		$response = $ua->request(GET $url);
		if (!$response->is_success) {
			die "#error:$0: GET MENU $url : " . $response->status_line;
		};

		$response->decoded_content =~ /rowUnMarked(?<data>.*?)\/tr>/s;

		$data = $+{data};
	} until ($data =~ /Done/ || $sleeptime*=2 > 32);

	# did the report actually finish or are we just insane
 	if ($sleeptime > 32) {
 		warn "#error: $0: Report never finished? Try again for $collegeid";
 	}
 	else {

		# get the href for the tab delimted report
		$data =~ /href="(?<href>.*?)"/;

		$url='https://secure.safaribooksonline.com/bo3/' . $+{href};
		$response = $ua->request(GET $url);
		if (!$response->is_success) {
			die "#error:$0: GET MENU $url : " . $response->status_line;
		};

		$content = $response->decoded_content;
		$content =~ s/\r//g;	# strip returns

		# save contents for possible debugging
		savecontent('Safari', "$collegeid", $content);

		my @contents = split("\n",$content);

		# go through each line of data (except header line) and update db
		foreach my $contents (@contents[1..$#contents]) {

			# ignore blank lines
			next if ($contents eq '');

			# TSV file
			my @columns = split("\t",$contents);

			# an actual date YYYYMMDD, have to add '-' between parts for SQL
			my $period = $columns[2];
			substr($period,4,0) = substr($period,6,0) = '-';
			print "$vendorid $collegeid $period 'Safari' $columns[4] $columns[3]\n";
			$sthstats->execute($vendorid, $collegeid, 'Safari', $period, $columns[4], 1);	# sessions
			$sthstats->execute($vendorid, $collegeid, 'Safari', $period, $columns[3], 2);	# searches
		}
	}
}

sub logout {
	$url = 'https://secure.safaribooksonline.com/bo3/BO.asp?logout=1';
	$response = $ua->request(GET $url);
}